/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.domain.auth;

import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.business.customer.facade.enums.auth.*;
import info.peigen.hotpot.common.core.domain.DomainObject;
import info.peigen.hotpot.common.core.enums.CertType;
import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;

import java.util.List;

import static info.peigen.hotpot.business.customer.facade.enums.auth.CustomerCertMethod.NORMAL;
import static info.peigen.hotpot.business.customer.facade.enums.auth.CustomerCertStatus.CERT_INIT;
import static info.peigen.hotpot.business.customer.facade.enums.auth.CustomerCertSubject.Personal;
import static info.peigen.hotpot.common.core.enums.CertType.Identity_Card;


/**
 * <b>(CustomerAuthentication)</b>
 * 身份验证领域模型
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/27
 */
@EqualsAndHashCode(callSuper = true)
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Accessors(chain = true)
public class CustomerAuthentication extends DomainObject {
    /** 认证基本信息 ？ */
    CustomerAuthenticationBase customerAuthenticationBase;
    /** 认证状态 */
    @Builder.Default
    CustomerCertStatus  customerCertStatus  = CERT_INIT;
    /** 客户认证主体 ？等能用到企业时，咱们已经发达了，可以暂时删除该字段 */
    @Builder.Default
    CustomerCertSubject customerCertSubject = Personal;
    /** 证件类型 */
    @Builder.Default
    CertType            customerCertType    = Identity_Card;
    /** 认证方式 ？ */
    @Builder.Default
    CustomerCertMethod  customerCertMethod  = NORMAL;
    /** 实名成员角色 ？ */
    @Builder.Default
    CustomerCertRole    customerCertRole    = CustomerCertRole.Control_People;
    /** 认证等级 */
    @Builder.Default
    CustomerCertLevel   level               = CustomerCertLevel.NORMAL;
    /** 备注 */
    String                          memo;
    /** 审核日志 */
    List<CustomerAuthenticationLog> logs;

    // ----------- 企业认证 -----------
    /** 成员信息 ？ 同上 */
    List<CustomerAuthenticationIdentity> identities;

    @Override
    public void check() {
        if (customerCertStatus != CERT_INIT) {
            switch (customerCertSubject) {
                case Personal:
                    Assert.notNull(customerAuthenticationBase, "个人实名信息不得为空!");
                    Assert.notNull(customerAuthenticationBase.getLegalRepresentative(), "个人实名信息不得为空!");
                    customerAuthenticationBase.getLegalRepresentative().check();
                    break;
                case Enterprise:
                    Assert.notNull(customerAuthenticationBase, "企业实名信息不得为空!");
                    Assert.notNull(customerAuthenticationBase.getLegalRepresentative(), "企业法人信息不得为空!");
                    customerAuthenticationBase.getLegalRepresentative().check();
                    Assert.notEmpty(customerAuthenticationBase.getEnterpriseName(), "企业名不得为空!");
                    Assert.notEmpty(customerAuthenticationBase.getBusinessScope(), "企业经营范围不得为空!");
                    Assert.notNull(customerAuthenticationBase.getRegisteredCapital(), "企业成立日期不得为空!");
                    Assert.notNull(customerAuthenticationBase.getEstablishmentDate(), "企业营业期限不得为空!");
                    break;
                case UNKNOWN:
                default:
                    break;
            }
        }
    }
}
